import Vue from 'vue'
import Vuex from 'vuex'
import util from '../common/util.js'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		userId: "",
		userInfo: null,
		openid: null,
		isShop: false,
		shopId: '',
		shopStatus: 0,
		unionid: null,
		fid: 0,
		userFid: 0,
		loginType: '',
		networkType: '',
		integral: 0,
		transfromClass: {
			'position': 'fixed',
			'bottom': 0,
			'top': 0,
			'left': 0,
			'right': 0,
			/* #ifndef APP-NVUE */
			'display': 'flex',
			/* #endif */
			'justify-content': 'center',
			'align-items': 'center',
			'z-index': 99
		},
		modeClass: ['zoom-in','slide-top']
	},
	mutations: {
		login(state) {
			state.hasLogin = true
		},
		setUserId(state, userId){
			state.userId = userId;
		},
		logout(state) {
			state.hasLogin = false
			state.openid = null
		},
		setOpenid(state, openid) {
			state.openid = openid
		},
		setIsShop(state, isShop){
			state.isShop = isShop==1?true:false
		},
		setShopId(state, shopId){
			state.shopId = shopId
		},
		setUserInfo(state, userInfo){
			state.userInfo = userInfo
		},
		setUnionId(state, unionid){
			state.unionid = unionid
		},
		setFId(state, fid){
			state.fid = fid
		},
		setUserFId(state, userFid){
			state.userFid = userFid
		},
		setLoginType(state, type){
			state.loginType = type
		},
		setNetworkType(state, networkType){
			state.networkType = networkType
		},
		setShopStatus(state, shopStatus){
			state.shopStatus = shopStatus
		},
		setIntegral(state,integral){
			state.integral = integral
		}
	},
	actions:{
		integral: function ({
			commit,
			state
		}){
			return new Promise((resolve,reject)=>{
				util.getAjax(util.apiUrl + '/get_user_integral',(res)=>{
					if(res.data){
						commit('setIntegral',res.data.integral)
						resolve(res.data.integral)
					}
				},{id: state.userId})
			})
		}
	}
})

export default store
